package com.tony.firewall.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;

import com.tony.firewall.data.PreferencesConfig;
import com.tony.firewall.R;

public class BlockListAdapter extends BaseAdapter {
	private ArrayList<String> mChildList = new ArrayList<String>(8);
	private Context mContext;
	private OnClickListener mListener;
	private TextWatcher mWatcher;
	private String mType = "";

	public BlockListAdapter(Context context) {
		mContext = context;
		initData();
	}

	public BlockListAdapter(Context context, String type) {
		mContext = context;
		mType = type;
		initData();
	}

	public void initData() {
		mChildList = PreferencesConfig.getBlockList(mContext, mType);

		// if (mChildList == null || mChildList.size() == 0) {
		// String[] list;
		// if (mType.equals(EditBlockActivity.TYPE_ADDRESS)) {
		// list = mContext.getResources().getStringArray(R.array.default_address_blacklist);
		// }
		// else {
		// list = mContext.getResources().getStringArray(R.array.default_block_keywords);
		// }
		// for (String address : list) {
		// mChildList.add(address);
		// }
		// }
	}

	public void setOnClickListener(OnClickListener listener) {
		mListener = listener;
	}

	public void setTextWatcher(TextWatcher watcher) {
		mWatcher = watcher;
	}
	
	// public void addChilds(MessageEntity[] list) {
	// for (MessageEntity entity : list) {
	// mChildList.add(entity);
	// }
	// notifyDataSetChanged();
	// }

	public void addChild(String address) {
		mChildList.add(address);
		notifyDataSetChanged();
	}

	public void updateChild(int position, String address) {
		if (position < mChildList.size() && mChildList.get(position) != null) {
			mChildList.set(position, address);
			notifyDataSetChanged();
		}
	}

	public void removeChild(String address) {
		mChildList.remove(address);
		notifyDataSetChanged();
	}

	public void removeChild(int postion) {
		mChildList.remove(postion);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mChildList.size();
	}

	@Override
	public String getItem(int position) {
		// TODO Auto-generated method stub
		return mChildList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<String> getList() {
		return mChildList;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_block_address, null);
		}

		EditText address = (EditText) convertView.findViewById(R.id.et_edit_address);
		address.setText(mChildList.get(position));
		address.addTextChangedListener(mWatcher);

		View delete = convertView.findViewById(R.id.img_delete_item);
		delete.setTag(position);
		delete.setOnClickListener(mListener);

		return convertView;
	}

}